<?php
/**
 * ***********************************************************************************
 * 咨询鲤 PC端知识店铺-直播相关
 * 
 * @author Mr.RJH
 * @date 2025-09-10
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\controller;

use app\BaseController;

use app\model\merchant\ConfigModel;
use app\model\product\ZxlKnowStoreProductModel;
use app\model\product\ZxlKnowStoreProductAttrModel;
use app\model\product\ZxlKnowStoreCategoryModel;

use app\model\user\ZxlKnowStoreCollectModel;

use app\service\ProductService;

class Live extends BaseController
{
    /**
     * 直播课列表
     * 
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isPost()) {

            $limit = $this->request->param('limit');
            $page = $this->request->param('page',1);
            $cate_level = $this->request->param('cate_level', 2);
            $cate_id = $this->request->param('category_id', '');
            
            $m_id = $this->request->m_id;
            try {
                $productService = new ProductService($m_id);
                $list = $productService->get_product_list(6, $limit, $page, $cate_id, $cate_level);
                foreach ($list['data'] as &$v) {
                    $v['head_imgs'] = $v['head_imgs'] ? explode(',',$v['head_imgs'])[0] : '/static/images/no_pic_1.png';
                    $v['tags'] = ZxlKnowStoreProductModel::get_tag_list($v['tags']);
                }

            } catch (\Exception $e) {
                return json(['code' => 1,'msg' => $e->getMessage()]);
            }
            return json(['code' => 0,'msg' => 'SUCCESS','count' => $list['total'],'data' => $list['data']]);
        }
        // 获取分类
        $first_category = ZxlKnowStoreCategoryModel::first_category($this->request->m_id, 2);

        $this->assign('first_category', $first_category);
        return $this->fetch();
    }

    /**
     * 直播课详情
     * 
     * @return mixed
     */
    public function detail()
    {
        $id = $this->request->param('id');
        $info = ZxlKnowStoreProductModel::getInfo($this->request->m_id, $id);
        if (!$info) {
            return redirect('/error/http404');
        }
        $info->visit_num += 1; // 增加访问量
        $info->save();

        $info['head_imgs'] = explode(',', $info['head_imgs']);
        
        if ($info['tags']) {
            $info['tags'] = json_decode($info['tags'], true);
        }
        $info['attr'] = ZxlKnowStoreProductAttrModel::where(['product_id' => $info['id'], 'status' => 1, 'is_delete' => 0])->order('sale_price asc')->select()->toArray();

        // 是否收藏
        $info['is_favorites'] = 0;
        $favorites = ZxlKnowStoreCollectModel::where(['user_id' => $this->user_id, 'product_id' => $info['id']])->find();
        if ($favorites) {
            $info['is_favorites'] = 1;
        }
        // 购课须知
        $notice = ConfigModel::getConfigValue('notice')[1];

        $productService = new ProductService($this->request->m_id);
        // 相关课程
        $related_list = $productService->rand_recommend_product(10, 6);

        $this->assign('info', $info);
        $this->assign('notice', $notice);
        $this->assign('related_list', $related_list);
        return $this->fetch();
    }
}